from PyQt5.QtCore import QThread, pyqtSignal, QRunnable, QObject
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
from utils.faceUtil import face_detect_and_save
import sys

class WorkerSignals(QObject):
    finished = pyqtSignal()
    progress = pyqtSignal(int)
    message = pyqtSignal(str)
    error = pyqtSignal(dict)
    result = pyqtSignal(dict)


class Worker(QRunnable):
    def __init__(self, photos):
        super().__init__()
        self.photos = photos
        self.signals = WorkerSignals()

    def run(self):
        for photo in self.photos:
            # 做人脸识别
            try:
                name = face_detect_and_save(photo['path'])
                if len(name) > 0:
                    self.signals.result.emit({'key': photo['id'], 'result': True})
                else:
                    self.signals.result.emit({'key': photo['id'], 'result': False})
            except Exception:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                self.signals.error.emit({'key': photo['id'], 'error': f"{exc_value}"})
        self.signals.finished.emit()
